<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.phone" placeholder="手机号码" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="编号"  align="center" >
        <template slot-scope="{row}">
          <span>  {{ row.uid }}</span>
        </template>
      </el-table-column>
   
      <el-table-column label="昵称" align="center"> 
        <template slot-scope="{row}">
          <span class="link-type">{{ row.nickname }}</span>
         
        </template>
      </el-table-column>
      <el-table-column label="头像" width="120px" align="center">
        <template slot-scope="{row}">
           <el-image
                style="width: 100px; height: 60px"
                :src="row.avatarUrl"
                fit="contain"
            ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="电话"  align="center">
        <template slot-scope="{row}">
          <span >{{ row.phone }}</span>
        </template>
      </el-table-column>
     
      <el-table-column label="权限类型"  align="center">
        <template slot-scope="{row}">
          
          <el-tag v-if="row.jurisdiction =='1'" effect="plain" type="danger"   size="mini" >老板</el-tag>
          <el-tag v-else-if="row.jurisdiction =='2'" effect="plain"  type="success" size="mini" >经理</el-tag>
          <el-tag v-else-if="row.jurisdiction =='3'" effect="plain"  type="warning" size="mini" >员工</el-tag>
        </template>
      </el-table-column>
       <el-table-column label="充值余额" width="160px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.balance }}</span>
        </template>
      </el-table-column>
        <el-table-column label="创建时间" width="160px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
     
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
           <!-- <el-dropdown  >
            <el-button type="primary" size="mini">
              更多<el-icon class="el-icon--right"><arrow-down /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="1">
                  <router-link :to="{path:'/project/projectAdd',query:{id:row.pid,type:'ziliao'}}">
                  项目资料
                </router-link>
                </el-dropdown-item>
                 <el-dropdown-item command="1">
                  <router-link :to="{path:'/project/projectAdd',query:{id:row.pid,type:'youshi'}}">
                  项目优势
                </router-link>
                </el-dropdown-item>
                 <el-dropdown-item command="1">
                  <router-link :to="{path:'/project/projectAdd',query:{id:row.pid,type:'fangan'}}">
                  投资方案
                </router-link>
                </el-dropdown-item>
                 <el-dropdown-item command="1">
                  <router-link :to="{path:'/project/projectAdd',query:{id:row.pid,type:'quanyi'}}">
                  项目权益
                </router-link>
                </el-dropdown-item>
               
              </el-dropdown-menu>
            </template>
          </el-dropdown> -->
           <el-popconfirm
              confirm-button-text="是"
              cancel-button-text="取消"
              icon-color="red"
              title="你是否确定移除商家权限?"
                @onConfirm="handleDelete(row,$index)"
            >
              <template #reference>
                <el-button size="mini" type="danger">移除</el-button>
              </template>
            </el-popconfirm>
      
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import { GQL } from '@/api/article'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination




export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },

  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        phone: undefined,
        type: undefined,
        level: undefined,
        sort: '+id'
      },
      showReviewer: false,
   
    }
  },
  created() {
     this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      let where = {}
    //   console.log('1111111111111111',this.listQuery)
       
        if(this.listQuery.page != 1){
             where = {"filter": {"isStore":{"equalTo":'1'}}, "first": this.listQuery.limit, "offset": this.listQuery.page*10-10, "orderBy": "CREATE_TIME_DESC"}
        }else{
         where = {"filter": {"isStore":{"equalTo":"1"}},"first": this.listQuery.limit, "offset": this.listQuery.page-1, "orderBy": "CREATE_TIME_DESC"}
        }
        if(this.listQuery.phone != undefined ){
            where.filter.phone = {like:"%"+this.listQuery.phone+"%"}
        }
        const query = {
            "operationName": "allSlUsers",
            "query": "query allSlUsers($filter: SlUserFilter,$offset: Int,$first: Int,$orderBy: [SlUsersOrderBy!] = [PRIMARY_KEY_ASC]) { allSlUsers( filter: $filter first:$first offset:$offset orderBy:$orderBy) { nodes { avatarUrl balance country level createTime province uid city nickname phone openid gender phoneAttestation idAttestation jurisdiction } totalCount } }",
            "variables":where
        }
       GQL(query).then(response => {
        this.list = response.data.allSlUsers.nodes
        this.total = response.data.allSlUsers.totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
      
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
     handleDelete(row, index) {
          const data = {
              isStore:'0'
          }
          let query = {
            "operationName": "updateSlUser",
            "query": "mutation updateSlUser($input: UpdateSlUserByUidInput!) { updateSlUserByUid(input: $input) { slUser { uid nickname } } }",
            "variables": {input: {uid: row.uid, slUserPatch:data}}
          }
          GQL(query).then((res) => {
               if(res.errors){
                  this.$notify({
                    title: '移除失败',
                    message: '移除失败',
                    type: 'error',
                    duration: 2000
                  })
              }else{
                  this.$notify({
                    title: '移除成功',
                    message: '移除成功',
                    type: 'success',
                    duration: 2000
                  })
                  this.list.splice(index, 1)
              }
      
          })
        
    }
  }
}
</script>
<style scoped>
.demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
